#include "rqueue.hpp"
#include <unistd.h>

typedef int val_type;

void* produce_routine(void* rq_)
{
    Ringqueue<val_type>* rq = static_cast<Ringqueue<val_type>*>(rq_);

    while(true)
    {
        int val = rand() % 20;
        rq->rq_push(val);
        std::cout << "生产者：" << val << std::endl;
        sleep(1);
    }

    return nullptr;
}

void* consumer_routine(void* rq_)
{
    Ringqueue<val_type>* rq = static_cast<Ringqueue<val_type>*>(rq_);
    while(true)
    {
        val_type val = 0;
        rq->rq_pop(val);
        std::cout << "消费者：" << val << std::endl;
    }
    val_type val = 0;
    rq->rq_pop(val);
    std::cout << "消费者：" << val << std::endl;
    return nullptr;
}

int main()
{
    Ringqueue<val_type>* rq = new Ringqueue<val_type>;

    pthread_t p, c;
    pthread_create(&p, nullptr, produce_routine, (void*)rq);
    pthread_create(&p, nullptr, consumer_routine, (void*)rq);

    pthread_join(p, nullptr);
    pthread_join(c, nullptr);
    return 0;
}